//
//  MainViewController.swift
//  SinaWeibo
//
//  Created by 吉亚雄 on 2016/11/8.
//  Copyright © 2016年 吉亚雄. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {


    @objc private func composeDidClick(){
        print(#function)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //由于tabBar是只读 不能够直接操作，如果要修改  可以使用KVC
        let mainTabBar = MainTabBar()
        //KVC 赋值
        setValue(mainTabBar, forKey: "tabBar")
        
        //添加子视图控制器
        addChildViewControllers()
        
        //classForCoder 打印函数的类名
        print(tabBar.classForCoder)
        
        //设置点击事件
        mainTabBar.composeBtn.addTarget(self, action:#selector(MainViewController.composeDidClick),for: .touchUpInside)
    }
    
    private func addChildViewControllers() {
        addChildViewController(vc: HomeTableViewController(), title: "首页", imagename: "tabbar_home")
        
        addChildViewController(vc: MessageTableViewController(), title: "消息", imagename: "tabbar_message_center")

        addChildViewController(vc: DiscoverTableViewController(), title: "发现", imagename: "tabbar_discover")

        addChildViewController(vc: ProfileTableViewController(), title: "我", imagename: "tabbar_profile")
}

    //重载（系统原有的方法重写）
    private func addChildViewController(vc:UIViewController,title:String,imagename:String) {
        
        self.tabBar.tintColor = UIColor.orange
        //实例化导航视图控制器
        let nav = UINavigationController(rootViewController: vc)
        
        //标题由内到外设置
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imagename)
        
        //设置选中后，为原始图片
       // vc.tabBarItem.selectedImage = UIImage(named: imagename + "_highlighted")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //设置选中后的字体颜色
       // vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: UIControlState.selected)
        
        //添加
        addChildViewController(nav)

    }
   
}
